package service

import (
	"context"
	pb "doctor-srv/basic/proto"
	"doctor-srv/basic/config"
	"doctor-srv/handler/model"
	"fmt"
	"time"
)

type PatientService struct {
	pb.UnimplementedPatientServer
}

// RegisterPatient 患者注册
func (s *PatientService) RegisterPatient(ctx context.Context, req *pb.RegisterPatientReq) (*pb.RegisterPatientResp, error) {
	// 检查手机号是否已存在
	var existPatient model.Patient
	err := existPatient.GetPatientByPhone(config.DB, req.Phone)
	if err == nil {
		return &pb.RegisterPatientResp{
			PatientId: 0,
			Message:   "手机号已存在",
		}, nil
	}

	// TODO: 验证短信验证码

	// 创建患者
	salt := GenerateSalt()
	patient := model.Patient{
		Name:     req.Name,
		Phone:    req.Phone,
		Password: Md5(req.Password + salt),
		Salt:     salt,
	}

	if err := patient.RegisterPatient(config.DB); err != nil {
		return &pb.RegisterPatientResp{
			PatientId: 0,
			Message:   "注册失败",
		}, err
	}

	return &pb.RegisterPatientResp{
		PatientId: int64(patient.ID),
		Message:   "注册成功",
	}, nil
}

// LoginPatient 患者登录
func (s *PatientService) LoginPatient(ctx context.Context, req *pb.LoginPatientReq) (*pb.LoginPatientResp, error) {
	// TODO: 验证图形验证码

	// 查询患者
	var patient model.Patient
	err := patient.GetPatientByPhone(config.DB, req.Phone)
	if err != nil {
		return &pb.LoginPatientResp{
			PatientId: 0,
			Token:     "",
		}, fmt.Errorf("手机号不存在")
	}

	// 验证密码
	if Md5(req.Password+patient.Salt) != patient.Password {
		return &pb.LoginPatientResp{
			PatientId: 0,
			Token:     "",
		}, fmt.Errorf("密码错误")
	}

	// TODO: 生成JWT Token
	token := fmt.Sprintf("token_%d_%d", patient.ID, time.Now().Unix())

	return &pb.LoginPatientResp{
		PatientId: int64(patient.ID),
		Token:     token,
		PatientInfo: &pb.PatientInfo{
			PatientId:      int64(patient.ID),
			Name:           patient.Name,
			Phone:          patient.Phone,
			AvatarUrl:      patient.AvatarUrl,
			IsVerified:     patient.IsVerified,
			HealthRecord:   patient.HealthRecord,
			AllergyHistory: patient.AllergyHistory,
		},
	}, nil
}

// UpdatePatientInfo 完善患者信息
func (s *PatientService) UpdatePatientInfo(ctx context.Context, req *pb.UpdatePatientInfoReq) (*pb.UpdatePatientInfoResp, error) {
	var patient model.Patient
	err := patient.GetPatientByID(config.DB, req.PatientId)
	if err != nil {
		return &pb.UpdatePatientInfoResp{
			Success: false,
			Message: "患者不存在",
		}, err
	}

	patient.HealthRecord = req.HealthRecord
	patient.AllergyHistory = req.AllergyHistory
	patient.MedicalHistory = req.MedicalHistory

	if err := patient.UpdatePatient(config.DB); err != nil {
		return &pb.UpdatePatientInfoResp{
			Success: false,
			Message: "更新失败",
		}, err
	}

	return &pb.UpdatePatientInfoResp{
		Success: true,
		Message: "更新成功",
	}, nil
}

// VerifyPatient 患者实名认证
func (s *PatientService) VerifyPatient(ctx context.Context, req *pb.VerifyPatientReq) (*pb.VerifyPatientResp, error) {
	var patient model.Patient
	err := patient.GetPatientByID(config.DB, req.PatientId)
	if err != nil {
		return &pb.VerifyPatientResp{
			Success: false,
			Message: "患者不存在",
		}, err
	}

	// TODO: 调用第三方接口验证身份证信息

	patient.IdCard = req.IdCard
	patient.RealName = req.RealName
	patient.IsVerified = true

	if err := patient.UpdatePatient(config.DB); err != nil {
		return &pb.VerifyPatientResp{
			Success: false,
			Message: "实名认证失败",
		}, err
	}

	return &pb.VerifyPatientResp{
		Success: true,
		Message: "实名认证成功",
	}, nil
}

// GetPatientInfo 获取患者信息
func (s *PatientService) GetPatientInfo(ctx context.Context, req *pb.GetPatientInfoReq) (*pb.GetPatientInfoResp, error) {
	var patient model.Patient
	err := patient.GetPatientByID(config.DB, req.PatientId)
	if err != nil {
		return nil, err
	}

	return &pb.GetPatientInfoResp{
		PatientInfo: &pb.PatientInfo{
			PatientId:      int64(patient.ID),
			Name:           patient.Name,
			Phone:          patient.Phone,
			AvatarUrl:      patient.AvatarUrl,
			IsVerified:     patient.IsVerified,
			HealthRecord:   patient.HealthRecord,
			AllergyHistory: patient.AllergyHistory,
		},
	}, nil
}

